package com.leoandroid.tool.toolsbox

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.leoandroid.tool.toolsbox.bean.XinHua
import com.leoandroid.tool.toolsbox.largeread.databinding.ActivityXinhuaDictionaryBinding
import com.leoandroid.tool.toolsbox.net.NetCallBack
import com.leoandroid.tool.toolsbox.net.NetRequest
import org.json.JSONObject

class XinHuaDictionaryActivity : AppCompatActivity(){

    var bindingF: ActivityXinhuaDictionaryBinding?=null
    val binding get() = bindingF!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindingF = ActivityXinhuaDictionaryBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.includeTitle.titleTv.text = "新华字典"
        binding.includeTitle.backIv.setOnClickListener {
            finish()
        }
        binding.searchBtn.setOnClickListener {
            val ed = binding.edit.text
            val url = "http://v.juhe.cn/xhzd/query?key=444c2eafd773a77603f4825d9d9fe2da&word=${ed}"
            NetRequest.asyncGet(url,object: NetCallBack {
                override fun onFailure(code: Int, e: Throwable?) {

                }

                override fun onSuccess(json: JSONObject?) {
                    Log.d("xxxxxxx","json>>"+json.toString())
                    val result = json?.getJSONObject("result")
                    runOnUiThread {
                        val xinhua = Gson().fromJson(result.toString(),XinHua::class.java)
                        binding.ziTv.text = "字："+xinhua.zi
                        binding.pyTv.text = "拼音："+xinhua.py
                        binding.wubiTv.text = "五笔："+xinhua.wubi
                        binding.pinyinTv.text = "读音："+xinhua.pinyin
                        binding.bushouTv.text = "部首："+xinhua.bushou
                        binding.bihuaTv.text = "笔画："+xinhua.bihua
                        binding.jijieContentTv.text = "简解："+xinhua.jijie.toString()
                        binding.xiangjieTv.text = "详解："+xinhua.xiangjie.toString()
//                    val zodiacBean = Gson().fromJson(result.toString(), ZodiacBean::class.java)

                    }
                }
            })
        }

    }
}